import com.live5.liveplayer.MediaPlayer;
import com.live5.liveplayer.Playlist;
import mx.utils.Delegate;
import mx.events.EventDispatcher;
class com.live5.liveplayer.LivePlayer extends MovieClip
{
	var defaultMediaType:String="video/x-flv";
	var playerInstance:MediaPlayer;
	var pl:Playlist;

	var placeHolder:MovieClip;
	function LivePlayer()
	{
	}
	function Play(plPath:String){
		if(plPath.indexOf("rss",0)>=0){
			pl=new Playlist(plPath,"rss");
		}
		if(plPath.indexOf("atom",0)>=0){
			pl=new Playlist(plPath,"atom");
		}
		pl.addEventListener("loaded",Delegate.create(this,OnPlaylistLoaded));
	}
	function SwitchPlayer(){
		
		if(playerInstance!=null)
		{
			playerInstance.removeMovieClip();
		}
		trace(pl.Current.Type);
		switch (pl.Current.Type){
			
			case "video/x-flv":
			playerInstance =MediaPlayer(this.attachMovie("com.live5.liveplayer.FlvPlayer","playerInstance",90));
			break;
			case "audio/mpeg":
			playerInstance =MediaPlayer(this.attachMovie("com.live5.liveplayer.Mp3Player","playerInstance",100));
			break;
			case "image/jpeg":
			playerInstance =MediaPlayer(this.attachMovie("com.live5.liveplayer.ImagePlayer","playerInstance",110));
			trace("should see this");
			break;
			default:
			playerInstance =MediaPlayer(this.attachMovie("com.live5.liveplayer.FlvPlayer","playerInstance",90));
			trace("never see this");
			break;
		}
		
		playerInstance.addEventListener("playlistComplete",Delegate.create(this,OnMediaComplete));
		playerInstance.addEventListener("switchPlayer",Delegate.create(this,OnSwitchPlayer));
		playerInstance.Play(pl);
	}
	function OnPlaylistLoaded(){
		placeHolder._visible=false;
		while(pl.Current==null&&pl.Next!=null){
			pl.MoveNext();
		}
		if(pl.Current!=null){
			SwitchPlayer();
		}
	}
	
	function OnMediaComplete(){
		trace("playlist complete");
		playerInstance.Stop();
	}
	function OnSwitchPlayer(){
		SwitchPlayer();
	}
}
